package GUILayer;

import ControlLayer.CtrEmp;
import ModelLayer.Employee;
import ModelLayer.WorksOn;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Casper
 * Date: 14-10-13
 * Time: 14:21
 * To change this template use File | Settings | File Templates.
 */
public class GUIEmployee {
    private JPanel contentPane;
    private JPanel pnlEmployee;
    private JPanel pnlWorksOn;
    private JTextField txtFirstName;
    private JTextField txtLastName;
    private JTextField txtSSN;
    private JTextField txtSalary;
    private JTextField txtSupervisor;
    private JTextField txtDepartment;
    private JPanel pnlLeftInfo;
    private JPanel pnlRightInfo;
    private JPanel pnlControls;
    private JButton btnClear;
    private JButton btnUpdate;
    private JButton btnInsert;
    private JButton btnFindProjects;
    private JButton btnSearch;
    private JLabel lblFirstName;
    private JLabel lblLastName;
    private JLabel lblSSN;
    private JLabel lblSalary;
    private JLabel lblSupervisor;
    private JLabel lblDepartment;
    private JList lstWorksOn;
    private JScrollPane scpWorksOnScrollPane;
    private JLabel lblError;

    public GUIEmployee() {
        btnSearch.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CtrEmp ctrEmp = new CtrEmp();

                String input = txtFirstName.getText().trim();
                System.out.println(input);
                    if(input.length() == 0){
                        input = txtLastName.getText().trim();
                        if(input.length() == 0)
                        {
                            input = txtSSN.getText().trim();
                            if(input.length() == 0)
                            {
                                //write error                                                w
                                lblError.setText("Please insert firstname, lastname or ssn");

                            }
                            else { //search by ssn
                                Employee empObj = ctrEmp.findBySsn(input);
                                if(empObj.getSsn().equals(""))
                                {
                                    lblError.setText("The inserted ssn does not exists");

                                }
                                else{
                                    clearFields();
                                    putValuesOnTheScreen(empObj);
                                }
                            }
                        }
                        else {//search by lname
                            Employee empObj = ctrEmp.findByLname(input);
                            if(empObj.getSsn().equals(""))
                            {
                                lblError.setText("The insert lname does not exists");
                            }
                            else{
                                clearFields();
                                putValuesOnTheScreen(empObj);
                            }
                        }
                    }
                    else { //search by fname
                        Employee empObj = ctrEmp.findByFname(input);

                        if(empObj.getSsn().equals(""))
                        {
                            lblError.setText("The insert fname does not exists");
                        }
                        else{
                            clearFields();
                            putValuesOnTheScreen(empObj);
                            lblError.setText("Employee found");
                        }
                    }
                }
            });
        btnFindProjects.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CtrEmp ctrEmp = new CtrEmp();
                String ssn = txtSSN.getText();
                if (ssn.length() == 0){
                    //write error
                    lblError.setText("Please insert ssn on the employee");
                }
                else {
                    ArrayList<WorksOn> empProject = ctrEmp.findProject(ssn);
                    buildList(empProject);
                    lblError.setText("Projects found");
                }
            }
        });
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame("GUIEmployee");
        frame.setContentPane(new GUIEmployee().contentPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setMinimumSize(new Dimension(600,520));
        frame.setVisible(true);
    }

    //Clearing all fields
    private void clearFields() {
        txtFirstName.setText("");
        txtLastName.setText("");
        txtSSN.setText("");
        txtSalary.setText("");
        txtSupervisor.setText("");
        txtDepartment.setText("");
        lblError.setText("");
        String[] empty = new String[5];
        lstWorksOn.setListData(empty);
    }

    //Putting values on screen
    private void putValuesOnTheScreen(Employee empObj) {
        DecimalFormat fmt = new DecimalFormat("####.##");
        txtFirstName.setText(empObj.getFname());
        txtLastName.setText(empObj.getLname());
        txtSSN.setText(empObj.getSsn());
        txtSalary.setText(String.valueOf(fmt.format(empObj.getSalary())));
        txtSupervisor.setText(empObj.getSupervisor().getFname());
        txtDepartment.setText(empObj.getDept().getDname());
    }

    //Building WorksOn jList
    private void buildList(ArrayList<WorksOn> empProjects) {
        String[] projects = new String[empProjects.size()];
        int index = 0;
        for(WorksOn wobj : empProjects)
        {
            String name = wobj.getProject().getName();
            if(name.length() < 15){
                int length = name.length();
                while (length < 20)
                {
                    name = name + "  ";
                    length++;
                }
            }
            projects[index] =(name + "   " + wobj.getHours());
            index++;
        }
        lstWorksOn.setListData(projects);
    }
}
